package com.zq.qq2.service;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

import com.zq.qq2.Constant;
import com.zq.qq2.R;
import com.zq.qq2.libsupersu.RootUtil;


/**
 *
 *
 */
public class FloatBackService extends Service {
    //定义浮动窗口布局
    LinearLayout mFloatLayout;
    WindowManager.LayoutParams wmParams;
    //创建浮动窗口设置布局参数的对象
    WindowManager mWindowManager;

    Button mRun;

    @Override
    public void onCreate() {
        super.onCreate();
        if ( !Constant.getSP(this).getBoolean("backshow", true) ) {
            this.stopSelf();
            return;
        }
        createFloatView();

        moveObject();

        mRun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ( Constant.getSP(FloatBackService.this).getBoolean("move", false) )
                    return;
                new Thread(){
                    @Override
                    public void run() {
                        RootUtil.Get().execute("input keyevent 4", null);
                    }
                }.start();
            }
        });

        mRun.setText("返回");

    }
    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        Intent notificationIntent = new Intent();
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        Notification noti = new Notification.Builder(this)
                .setContentTitle("返回键")
                .setContentText("")
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pendingIntent)
                .build();
        //startForeground(12346, noti);
        return Service.START_STICKY;
    }

    private void createFloatView() {

        wmParams = new WindowManager.LayoutParams();
        mWindowManager = (WindowManager)getApplication().getSystemService(getApplication().WINDOW_SERVICE);
        wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        //设置图片格式，效果为背景透明
        wmParams.format = PixelFormat.RGBA_8888;
        //设置浮动窗口不可聚焦（实现操作除浮动窗口外的其他可见窗口的操作）
        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        //调整悬浮窗显示的停靠位置为左侧置顶
        wmParams.gravity = Gravity.TOP | Gravity.LEFT;
        // 以屏幕左上角为原点，设置x、y初始值，相对于gravity

        int x = Constant.getSP(this).getInt("bx", 0);
        int y = Constant.getSP(this).getInt("by", 420);
        wmParams.x = x;
        wmParams.y = y;

        // 设置悬浮窗口长宽数据
        wmParams.width = 180;
        wmParams.height = 90;

        wmParams.width = 240;
        wmParams.height = 120;
        LayoutInflater inflater = LayoutInflater.from(getApplication());
        //获取浮动窗口视图所在布局
        mFloatLayout = (LinearLayout) inflater.inflate(R.layout.float_home, null);
        //添加mFloatLayout
        mWindowManager.addView(mFloatLayout, wmParams);
        //浮动窗口按钮
        mRun = (Button)mFloatLayout.findViewById(R.id.home);

        mFloatLayout.measure(View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
                .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

    }
    private void moveObject() {
        mRun.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                move(event);
                return false;
            }
        });

        mFloatLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                move(event);
                return false;
            }
        });
    }
    private void move(MotionEvent event) {
        if ( !Constant.getSP(FloatBackService.this).getBoolean("move", false) )
            return;
        if( event.getAction() == MotionEvent.ACTION_MOVE ) {
            wmParams.x = (int) event.getRawX() - 90;
            //减25为状态栏的高度
            wmParams.y = (int) event.getRawY() - 60;
            //刷新
            mWindowManager.updateViewLayout(mFloatLayout, wmParams);
            Constant.getSP(this).edit().putInt("bx", wmParams.x).putInt("by", wmParams.y).commit();
        }
    }
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if(mFloatLayout != null) {
            //移除悬浮窗口
            mWindowManager.removeView(mFloatLayout);
        }
    }
}
